package xin.marcher.module.lifecycle.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.lifecycle.domain.dto.ItemExpriResultDTO;
import xin.marcher.module.lifecycle.domain.dto.ItemExpriResultToExcelDTO;
import xin.marcher.module.lifecycle.domain.dto.ItemPeriodStageDTO;
import xin.marcher.module.lifecycle.domain.request.ItemExpriReslutPageRequest;
import xin.marcher.module.lifecycle.domain.request.ItemPeriodStagePageQueryRequest;
import xin.marcher.module.lifecycle.service.ItemPeriodStageService;
import xin.marcher.module.lifecycle.util.ExcelUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/ItemPeriodStage")
public class ItemPeriodStageController {

    @Autowired
    private ItemPeriodStageService itemPeriodStageService;

    /**
     * 根据商品id查询商品生命周期
     *
     * @param request
     * @return
     */
    @RequestMapping("/pageItemStage")
    JsonResult<PageResult<ItemPeriodStageDTO>> queryProductPeriodBatch(ItemPeriodStagePageQueryRequest request) {
        try {
            PageResult<ItemPeriodStageDTO> page = itemPeriodStageService.queryItemPeriodStageByPage(request);
            return JsonResult.buildSuccess(page);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据商品id查询商品考核结果（包括可采可补信息）
     *
     * @param request
     * @return
     */
    @RequestMapping("/pageItemExpri")
    JsonResult<PageResult<ItemExpriResultDTO>> queryProductPeriodMarketExpriBatch(@RequestBody ItemExpriReslutPageRequest request) {
        try {
            PageResult<ItemExpriResultDTO> itemExpriResults = itemPeriodStageService.queryItemExpriResultByPage(request);
            return JsonResult.buildSuccess(itemExpriResults);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据单条商品id查询商品当前所处生命周期状态
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/queryItemStage")
    JsonResult<ItemPeriodStageDTO> queryProductPeriodStage(String itemId) {
        try {
            ItemPeriodStageDTO itemPeriodStageDTO = itemPeriodStageService.queryProductPeriod(itemId);
            return JsonResult.buildSuccess(itemPeriodStageDTO);
        } catch (Exception e) {
            log.error("system error: param={}", JSON.toJSONString(itemId), e);
            return JsonResult.buildError(e.getMessage());
        }
    }


    /**
     * 导出数据到Excel
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportItemExpriToExcel")
    public void exportExcel(@RequestBody ItemExpriReslutPageRequest request, HttpServletResponse response) throws IOException {
        try {
            List<ItemExpriResultToExcelDTO> itemExpriResults = itemPeriodStageService.queryItemExpriResultBatch(request);
            String title = "商品考核结果";
            String sheetName = title;
            String fileName = title + System.currentTimeMillis();
            log.info("result:{}", itemExpriResults);
            ExcelUtils.exportExcel(itemExpriResults, title, sheetName, ItemExpriResultToExcelDTO.class, fileName, response);
        } catch (BaseBizException baseBizException) {
            log.error("biz error: request={}", JSON.toJSONString(request), baseBizException);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
        }
    }

    /**
     * 根据商品itemId查询当前商品考核结果及可采可补可售信息
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/queryItemExpri")
    JsonResult<ItemExpriResultDTO> queryProductPeriodMarketExpri(String itemId) {
        try {
            ItemExpriResultDTO skuExpriResultDTO = itemPeriodStageService.queryItemExpriResultByItemId(itemId);
            return JsonResult.buildSuccess(skuExpriResultDTO);
        } catch (Exception e) {
            log.error("system error: param={}", JSON.toJSONString(itemId), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
